// +----------------------------------------------------------------------
// | CmsWing [ 美业管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 psi All rights reserved.
// +----------------------------------------------------------------------
// | Author: huang <2374266244@qq.com>
// +----------------------------------------------------------------------
'use strict';
import Base from './base.js';

export default class extends Base {
  /**
   * index action
   * @return {Promise} []
   */
	async editAction(){
		if(this.isPost){
			let username = this.post('username');
			let name = this.post('name');
			let phone = this.post('phone');
			let remarks = this.post('remarks');
			let headimg = this.post('headimg');
			let id = this.post('id');
			
			let res = await this.model("admin").where({id:id}).update({username:username,name:name,phone:phone,remarks:remarks,headimg:headimg});
			
			if(res){
				await this.update_session();
				return this.success({info:"编辑成功！"});
			}else{
				return this.fail('编辑失败！');	
			}
		}
		return this.display();
	}
	
	async changepasAction(){
		if(this.isPost){
			let oldpassword = think.md5(this.post('oldpassword'));
			let password = think.md5(this.post('password'));
			let rpassword = think.md5(this.post('rpassword'));
			
			if(password !== rpassword){
				return this.fail('两次输入的密码不一致！');
			}
			
			//判断旧密码是否正确
			if(oldpassword !== this.userinfo.password){
				return this.fail('旧密码填写错误！');
			}
			let res = await this.model("admin").where({id:this.userinfo.id}).update({password:password});
			if(res){
				await this.update_session();
				return this.success({info:"修改成功！"});
			}else{
				return this.fail('修改失败！');	
			}
		}else{
			return this.display();
		}	
	}
  
}